package mylab.starters.mybatis.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import mylab.starters.mybatis.OrderlyPlugin;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@MapperScan("**.mapper.**")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(DbAutoConfiguration.class)
public class DbPluginAutoConfiguration {
    private final List<OrderlyPlugin> innerPlugins;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        innerPlugins.stream().sorted().collect(Collectors.toList()).forEach(interceptor::addInnerInterceptor);
        return interceptor;
    }

}
